<?php

namespace common\models;

use Yii;

/**
 * This is the model class for table "chat_msg".
 *
 * @property string $id 消息ID
 * @property int $chat_type 会话类型0好友消息1群消息
 * @property string $chat_id 会话ID
 * @property string $chat_name 会话名称
 * @property string $sender 发送人
 * @property int $senderid 发送人ID
 * @property string $sendee 接收人
 * @property int $sendeeid 接收人ID
 * @property int $msg_type 消息类型
 * @property string $content 消息内容
 * @property int $isread 是否已读0未读1已读
 * @property string $readtime 阅读时间
 * @property string $addtime 消息时间
 */
class ChatMsg extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'chat_msg';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['chat_id', 'senderid', 'sendeeid', 'readtime', 'addtime'], 'integer'],
            [['sender', 'senderid', 'content', 'addtime'], 'required'],
            [['chat_type'], 'string', 'max' => 3],
            [['chat_name'], 'string', 'max' => 100],
            [['sender', 'sendee'], 'string', 'max' => 80],
            [['msg_type'], 'string', 'max' => 2],
            [['content'], 'string', 'max' => 1000],
            [['isread'], 'string', 'max' => 1],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => '消息ID',
            'chat_type' => '会话类型0好友消息1群消息',
            'chat_id' => '会话ID',
            'chat_name' => '会话名称',
            'sender' => '发送人',
            'senderid' => '发送人ID',
            'sendee' => '接收人',
            'sendeeid' => '接收人ID',
            'msg_type' => '消息类型',
            'content' => '消息内容',
            'isread' => '是否已读0未读1已读',
            'readtime' => '阅读时间',
            'addtime' => '消息时间',
        ];
    }
    
    //获取会话的最后一条消息
    public static function getLastMsg($chatid){
        $sql = "select * from chat_msg where chat_id=$chatid order by addtime desc";
        $command = \Yii::$app->db->createCommand($sql);
        return $command->queryOne();
    }
    
    //获取会话的未读消息数量
    public static function getUnreadNum($chatid,$userid){
        $sql = "select count(id) unreadnum from chat_msg where chat_id=$chatid and sendeeid=$userid and isread=0";
        $command = \Yii::$app->db->createCommand($sql);
        $data = $command->queryOne();
        return $data['unreadnum'];
    }
    
    public static function getHistoryMsg($chatid,$min=0){
        $condition = "";
        if($min != 0){
            $condition = " and id<$min";
        }
        $sql = "select * from chat_msg where chat_id=$chatid $condition order by addtime desc limit 0,10";
        $command = \Yii::$app->db->createCommand($sql);
        return $command->queryAll();
    }
    
    public static function setToReaded($chatid,$userid){
        $readtime = time();
        $sql = "update chat_msg set isread=1,readtime=$readtime where chat_id=$chatid and sendeeid=$userid and isread=0";
        $command = \Yii::$app->db->createCommand($sql);
        return $command->execute();
    }
}
